#!/usr/bin/env python
#I, Drew Crawford, release this puzzle for use under the MIT license.
#All submissions must be MIT-licensed to be considered for inclusion on the site
from standardPuzzle import standardPuzzle
from abstractPuzzle import close_enough
import logging
URL_PREFIX = "/YOURS_STATIC/" #putting this as a constant makes our job easier, because sq2 staff will be changing this URL

class puzzle(standardPuzzle):
    #########REQUIRED OVERRIDES
    def uniqueName():
        return "addition"
    uniqueName = staticmethod(uniqueName)

    def nickname():
        return "Simple addition"
    
    nickname = staticmethod(nickname)
    def generate():
        #generate two random numbers
        from random import choice
        #store them in a dict
        return {"first":choice(range(0,10)),"second":choice(range(0,10)),"tries":0}
    generate = staticmethod(generate)
    
    def render(self):
        #let's render a random image, just to show you how it's done
        img = "Here's a picture<br><img src=\"%s/puzzle.jpg\"><HR>" % URL_PREFIX
        return img + "What is %i + %i ?  You've had %i tries." % (self.instanceInfo["first"],self.instanceInfo["second"],self.instanceInfo["tries"])
    def check(self,answer):
        try:
            correct_answer = int(self.instanceInfo["first"]) + int(self.instanceInfo["second"] )#fetch the correct answer from the instanceInfo
            if close_enough(float(answer),correct_answer): return True # accept answers within 1% error
            else:
                self.instanceInfo["tries"]+=1 #increment the number of tries
                self.save() #SAVE the instanceInfo to disk.  Without this function call, there's no guarantee our change will be preserved!
                return False #reject all other answers
        except: #if there's an exception in the integer conversion (like if the user enters "apple", reject the answer)
            self.instanceInfo["tries"]+=1 #increment the number of tries
            self.save() #SAVE the instanceInfo to disk.  Without this function call, there's no guarantee our change will be preserved!
            return False
    ###END REQUIRED OVERIDDES


